/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import bean.Panne;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import bean.Vehicule;
import dao.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author mezouari
 */
public class PanneJpaController implements Serializable {

    public PanneJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Panne panne) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vehicule idVehicule = panne.getIdVehicule();
            if (idVehicule != null) {
                idVehicule = em.getReference(idVehicule.getClass(), idVehicule.getId());
                panne.setIdVehicule(idVehicule);
            }
            em.persist(panne);
            if (idVehicule != null) {
                idVehicule.getPanneList().add(panne);
                idVehicule = em.merge(idVehicule);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Panne panne) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Panne persistentPanne = em.find(Panne.class, panne.getId());
            Vehicule idVehiculeOld = persistentPanne.getIdVehicule();
            Vehicule idVehiculeNew = panne.getIdVehicule();
            if (idVehiculeNew != null) {
                idVehiculeNew = em.getReference(idVehiculeNew.getClass(), idVehiculeNew.getId());
                panne.setIdVehicule(idVehiculeNew);
            }
            panne = em.merge(panne);
            if (idVehiculeOld != null && !idVehiculeOld.equals(idVehiculeNew)) {
                idVehiculeOld.getPanneList().remove(panne);
                idVehiculeOld = em.merge(idVehiculeOld);
            }
            if (idVehiculeNew != null && !idVehiculeNew.equals(idVehiculeOld)) {
                idVehiculeNew.getPanneList().add(panne);
                idVehiculeNew = em.merge(idVehiculeNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = panne.getId();
                if (findPanne(id) == null) {
                    throw new NonexistentEntityException("The panne with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Panne panne;
            try {
                panne = em.getReference(Panne.class, id);
                panne.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The panne with id " + id + " no longer exists.", enfe);
            }
            Vehicule idVehicule = panne.getIdVehicule();
            if (idVehicule != null) {
                idVehicule.getPanneList().remove(panne);
                idVehicule = em.merge(idVehicule);
            }
            em.remove(panne);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Panne> findPanneEntities() {
        return findPanneEntities(true, -1, -1);
    }

    public List<Panne> findPanneEntities(int maxResults, int firstResult) {
        return findPanneEntities(false, maxResults, firstResult);
    }

    private List<Panne> findPanneEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Panne.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Panne findPanne(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Panne.class, id);
        } finally {
            em.close();
        }
    }

    public int getPanneCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Panne> rt = cq.from(Panne.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
